package com.quexin.pickmedialib;

import android.content.Context;
import android.content.Intent;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class PickerMediaContract extends
        ActivityResultContract<PickerMediaParameter, PickerMediaResutl> {

    private int requestCode = PickerMediaParameter.DEFAULT_REQUEST_CODE;

    @NonNull
    @Override
    public Intent createIntent(@NonNull Context context, PickerMediaParameter input) {
        Intent intent = new Intent(context, PickerMediaActivity.class);
        if (input != null) {
            requestCode = input.getRequestCode();
            intent.putExtra(PickerMediaParameter.PICKER_MEDIA, input);
            intent.putExtra(PickerMediaParameter.PICKER_MEDIA_DATA, input.getPickerData());
        }
        return intent;
    }

    @Override
    public PickerMediaResutl parseResult(int resultCode, @Nullable Intent intent) {
        PickerMediaResutl result = new PickerMediaResutl(requestCode);
        if (resultCode == PickerMediaParameter.RESULT_OK && intent != null) {
            ArrayList<MediaModel> pickerData = intent.getParcelableArrayListExtra(
                    PickerMediaParameter.PICKER_MEDIA);
            if (pickerData != null && pickerData.size() > 0) {
                result.addData(pickerData);
            }
        }
        return result;
    }
}
